package com.centon.auto_code.controller;

import com.centon.auto_code.comm.ServiceCode;
import com.centon.auto_code.domain.SysUser;
import com.centon.auto_code.domain.gen.SysUserCriteria;
import com.centon.auto_code.mapper.SysUserMapper;
import com.centon.auto_code.security.LoginUser;
import com.centon.auto_code.service.SysAuthorityService;
import com.centon.auto_code.system.vo.SysUserVo;
import com.centon.auto_code.system.vo.gen.SuperSysUserVo.CommController_adminLogin;
import com.centon.auto_code.util.Constants.LoginUserType;
import com.centon.auto_code.util.JWTHelperUtil;
import com.centon.auto_code.vo.BaseRes;
import com.centon.auto_code.vo.LoginResVo;
import java.util.List;
import java.util.Set;
import javax.annotation.Resource;
import javax.annotation.Title;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("comm")
public class CommController {

	private Logger log = LoggerFactory.getLogger(CommController.class);

	@Resource
	private SysUserMapper sysUserMapper;

	@Resource
	private JWTHelperUtil jWTHelperUtil;

    /**
     * @gen_345_lwl
     * 系统管理员登录
     * null
     * @param sysUserVo
    **/
    @Title("系统管理员登录")
    @ResponseBody
    @RequestMapping(value="admin/login", method=RequestMethod.POST)
    public BaseRes<LoginResVo> adminLogin(@RequestBody @Validated(CommController_adminLogin.class) SysUserVo sysUserVo, BindingResult bindingResult) {
		BaseRes<LoginResVo> res = new BaseRes<>();

		String username = sysUserVo.getUsername();
		String password = sysUserVo.getPassword();

		SysUserCriteria suCriteria = new SysUserCriteria();
		suCriteria.createCriteria()
				.andUsernameEqualTo(username)
				.andPasswordEqualTo(password)
				.andStatusEqualTo("enable");
		List<SysUser> users = sysUserMapper.selectByExample(suCriteria);

		if (!CollectionUtils.isEmpty(users)) {
			LoginResVo loginRes = new LoginResVo();
			SysUser sUser = users.get(0);
			loginRes.setId(sUser.getId());
			loginRes.setUsername(sUser.getUsername());
			loginRes.setType(LoginUserType.admin.name());

			String token = jWTHelperUtil.getToken(loginRes);

			log.info("token={}", token);

			LoginUser loginUser = jWTHelperUtil.verifyAdminToken(token);
			Set<String> authIds = loginUser.getAuthIds();

			log.info("username={}, token={}", username, token);
			loginRes.setToken(token);
			loginRes.setAuthIds(authIds);

			res.setData(loginRes);
			res.setServiceCode(ServiceCode.SUCCESS);
			return res;
		}
		return new BaseRes<>(ServiceCode.LOGIN_ERROR);
    }
}